package cz.idsv.web.mvc.controller;

/**
 * Registration Controller.
 * 
 * @author Jan Borzensky
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.idsv.web.mvc.model.entity.RegistrationEntity;
import cz.idsv.web.mvc.model.service.RegistrationService;



/**
 * Registration controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = {"registrationEntity"})
public class RegistrationController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String REGISTRATION_ENTITY = "registrationEntity";
	
	protected static Logger logger = Logger.getLogger("RegistrationController"); 
	
	@Autowired
	private RegistrationService registrationService;
	
	/**
     * <p>Searches for all registration and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/registration/list'.</p>
     */
    @RequestMapping(value="/registration/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<RegistrationEntity> search() {
    	logger.debug("Received request to show all registration"); 
    	
        return registrationService.findRegistration();
    }
    
	/**
     * <p>Create new or update old Registration 
     * <code>AddressEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/registration/form'.</p>
     */
    @RequestMapping(value="/registration/form", method=RequestMethod.GET)
    public @ModelAttribute(REGISTRATION_ENTITY)  RegistrationEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new RegistrationEntity();
        return registrationService.findRegistrationById(id);
    }
    
	/**
     * <p>Save new or update old registration 
     * <code>RegistrationEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/registration/saveForm'.</p>
     */
    @RequestMapping(value="/registration/saveForm", method=RequestMethod.POST)
    public String saveRegistrationForm(RegistrationEntity registrationEntity, Model model) {
    	if(registrationEntity != null) 
    		registrationService.save(registrationEntity);
    	return "redirect:/registration/list.html";
    }
    
    /**
     * <p>Deletes a registration.</p>
     * 
     * <p>Expected HTTP POST and request '/registration/delete'.</p>
     */
    @RequestMapping(value="/registration/delete", method=RequestMethod.POST)
    public String delete(RegistrationEntity registrationEntity) {
    	logger.debug("Received request to delete registration with id:"+ registrationEntity.getId());
    	registrationService.delete(registrationEntity);
        return "redirect:/registration/list.html";
    }
    
}
